package com.fenxiangle.yueding.feature.message.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.entity.bo.UnMsgBo;
import com.fenxiangle.yueding.entity.bo.UnReadCountBo;
import com.fenxiangle.yueding.framework.api.HomeApi;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import me.leolin.shortcutbadger.ShortcutBadger;


public class MessageFragment extends BaseFragment {

    Unbinder unbinder;
    @BindView(R.id.tv_msg_sys)
    TextView tvMsgSys;
    @BindView(R.id.tv_msg_order)
    TextView tvMsgOrder;

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_message;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initData() {


    }


    @Override
    protected void initEvent() {

    }

    @Override
    public void onStart() {
        super.onStart();
        getMsgCount();
    }

    private void getMsgCount() {
        AM.api().createApiService(HomeApi.class)
                .selectDiffMessageCount(new UnMsgBo(1 + ""))
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<UnReadCountBo>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(UnReadCountBo msgBos) {

                        if (msgBos.getSystemMessage() > 0) {
                            tvMsgSys.setVisibility(View.VISIBLE);
                            tvMsgSys.setText(msgBos.getSystemMessage() + "");
                        } else {
                            tvMsgSys.setVisibility(View.GONE);
                        }


                        if (msgBos.getOrderMessage() > 0) {
                            tvMsgOrder.setVisibility(View.VISIBLE);
                            tvMsgOrder.setText(msgBos.getOrderMessage() + "");
                        } else {
                            tvMsgOrder.setVisibility(View.GONE);
                        }

                        if(msgBos.getUserCenterTotal()>0){
                            ShortcutBadger.applyCount(getActivity(), msgBos.getUserCenterTotal());
                        }else {
                            ShortcutBadger.removeCount(getActivity());
                        }

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    @OnClick({R.id.btn_msg_system, R.id.btn_msg_order})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_msg_system:
                startActivity(new Intent(getActivity(), SysMsgActivity.class));
                break;
            case R.id.btn_msg_order:
                startActivity(new Intent(getActivity(), OrderMsgActivity.class));
                break;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
